package com.udaye.movie.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.udaye.library.pullloadlibrary.CommonViewHolder;
import com.udaye.library.pullloadlibrary.RecyclerViewCommonAdapter;
import com.udaye.movie.R;
import com.udaye.movie.entity.Top250Bean;
import com.udaye.movie.ui.main.MovieDetailActivity;
import com.udaye.movie.util.CelebrityUtil;
import com.udaye.movie.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * top 250
 * <p/>
 * Created on 16/7/12.
 */
public class TopAdapter extends RecyclerViewCommonAdapter<Top250Bean.SubjectsBean> {
    /**
     * @param mContext 上下文对象
     * @param list     数据List
     */
    public TopAdapter(Context mContext, List<Top250Bean.SubjectsBean> list) {
        super(mContext, list, R.layout.item_simple_subject_layout);
    }

    public void update(List<Top250Bean.SubjectsBean> list) {
        addList((ArrayList<Top250Bean.SubjectsBean>) list);
    }

    @Override
    public void onListBindViewHolder(CommonViewHolder holder, int position) {
//        final Top250Bean.SubjectsBean subjectsBean = getItem(position);
//        holder.setText(R.id.tv_movie_name, subjectsBean.getTitle());
//        holder.setText(R.id.tv_movie_score, String.format(mContext.getString(R.string.tip_score), String.valueOf(subjectsBean.getRating().getAverage())));
//        holder.setText(R.id.tv_movie_date, String.format(mContext.getString(R.string.tip_movie_date), subjectsBean.getYear()));
//        ImageView imageView = (ImageView) holder.itemView.findViewById(R.id.iv_movie_icon);
//        Glide.with(mContext)
//                .load(mList.get(position).getImages().getLarge())
//                .placeholder(android.R.color.white)
//                .into(imageView);

        final Top250Bean.SubjectsBean sub = getItem(position);

        ImageView image_film = holder.itemView.findViewById(R.id.iv_item_simple_subject_image);
        LinearLayout layout_rating = holder.itemView.findViewById(R.id.ll_item_simple_subject_rating);
        RatingBar rating_bar = holder.itemView.findViewById(R.id.rb_item_simple_subject_rating);
        TextView text_rating = holder.itemView.findViewById(R.id.tv_item_simple_subject_rating);
        TextView text_collect_count = holder.itemView.findViewById(R.id.tv_item_simple_subject_count);
        TextView text_title = holder.itemView.findViewById(R.id.tv_item_simple_subject_title);
        TextView text_original_title = holder.itemView.findViewById(R.id.tv_item_simple_subject_original_title);
        TextView text_genres = holder.itemView.findViewById(R.id.tv_item_simple_subject_genres);
        TextView text_director = holder.itemView.findViewById(R.id.tv_item_simple_subject_director);
        TextView text_cast = holder.itemView.findViewById(R.id.tv_item_simple_subject_cast);


        layout_rating.setVisibility(View.VISIBLE);
        float rate = (float) sub.getRating().getAverage();
        rating_bar.setRating(rate / 2);
        text_rating.setText(String.format("%s", rate));
        text_collect_count.setText(mContext.getString(R.string.collect));
        text_collect_count.append(String.format("%d", sub.getCollect_count()));
        text_collect_count.append(mContext.getString(R.string.count));

        String title = sub.getTitle();
        String original_title = sub.getOriginal_title();
        text_title.setText(title);
        if (original_title.equals(title)) {
            text_original_title.setVisibility(View.GONE);
        } else {
            text_original_title.setText(original_title);
            text_original_title.setVisibility(View.VISIBLE);
        }
        text_genres.setText(StringUtil.getListString(sub.getGenres(), ','));
        text_director.setText(StringUtil.getSpannableString(
                mContext.getString(R.string.directors), Color.GRAY));
        text_director.append(CelebrityUtil.list2String(sub.getDirectors(), '/'));
        text_cast.setText(StringUtil.getSpannableString(
                mContext.getString(R.string.casts), Color.GRAY));
        text_cast.append(CelebrityUtil.list2String2(sub.getCasts(), '/'));
        Glide.with(holder.itemView.getContext())
                .load(sub.getImages().getLarge())
                .into(image_film);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(MovieDetailActivity.getCallingIntent(mContext, sub.getId()));
            }
        });

    }
}
